package cn.tedu.micro.demo.order.service;

import cn.tedu.micro.demo.common.pojo.po.OrderInfoPO;
import cn.tedu.micro.demo.common.pojo.vo.GunInfoVO;
import cn.tedu.micro.demo.order.feign.DeviceFeignClient;
import cn.tedu.micro.demo.order.mapper.OrderInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService{
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private DeviceFeignClient deviceFeignClient;
    @Override
    public String createOrder(Integer gunId, Integer userId) {
        log.info("mapper接口类实现:{}",orderInfoMapper.getClass().getName());
        log.info("feign接口类实现:{}",deviceFeignClient.getClass().getName());
        //用户认为枪状态可用的 但是订单应该检查是否可用
        GunInfoVO data = deviceFeignClient.getGunStatus(gunId).getData();
        //非空 且 状态为1时才创建订单
        if(data!=null || data.getStatus()!=1){
            log.info("枪数据条件不符合要求，禁止下单");
            return null;
        }
        log.info("查询到枪状态数据：{}", data);
        //insert into order_info (gun_id,user_id,bill_id,create_time,update_time)
        //values (#{},#{},#{},#{},#{})
        //bill_id属于每个订单唯一编号,满足特点 唯一 有序 可读(雪花算法工具)
        String billId= UUID.randomUUID().toString();
        //准备一个新增po
        OrderInfoPO orderPO=new OrderInfoPO();
        orderPO.setUserId(userId);
        orderPO.setGunId(gunId);
        orderPO.setBillId(billId);
        orderPO.setCreateTime(new Date());
        orderPO.setUpdateTime(new Date());
        //调用insert
        orderInfoMapper.insert(orderPO);
        return billId;
    }
}
